package rule

import (
	"regexp"
)

func init() {
	Register(&dateRule{
		reg: regexp.MustCompile(`^\d{4}[\.\-\_/]{0,1}\d{2}[\.\-\_/]{0,1}\d{2}$`),
	})
}

type dateRule struct {
	reg *regexp.Regexp
}

func (d *dateRule) Name() string {
	return "date"
}

func (d *dateRule) Check(in *RuleInput) (err error) {
	if v, ok := in.Value.(string); ok && v != "" && d.reg.FindStringIndex(v) != nil {
		return
	}
	return in.Error()
}
